package cn.gd.controller.Announcements;

import cn.gd.pojo.Announcements;
import cn.gd.pojo.Users;
import cn.gd.service.Announcements.AnnouncementService;
import cn.gd.utils.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wyh
 * @version 1.80
 */
@CrossOrigin
@RestController
@RequestMapping("/announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    @GetMapping("/announcementsGetAll/{pageNo}/{pageSize}")
    public Result<PageInfo<Announcements>> announcementsGetAll(@PathVariable Integer pageNo,
                                                              @PathVariable Integer pageSize) {
        return Result.ok(announcementService.announcementsGetAll(pageNo, pageSize));
    }

    @PostMapping("/announcementsAdd")
    public Result<Map<String, String>> announcementsAdd(@RequestBody Announcements announcements) {
        int num = announcementService.announcementsAdd(announcements);
        Map<String, String> map = new HashMap<>();
        if (num > 0) {
            map.put("result", "true");
        } else {
            map.put("result", "false");
        }
        return Result.ok(map);
    }

    @DeleteMapping("/announcementsDelete/{id}")
    public Result<Map<String, String>> announcementsDelete(@PathVariable Integer id) {
        int num = announcementService.announcementsDelete(id);
        Map<String, String> map = new HashMap<>();
        if (num > 0) {
            map.put("result", "true");
        } else {
            map.put("result", "false");
        }
        return Result.ok(map);
    }

    @GetMapping("/announcementsGetId/{uid}")
    public Result<Announcements> announcementsGetId(@PathVariable Integer uid) {
        return Result.ok(announcementService.announcementsGetId(uid));
    }

    @PutMapping("/announcementsUpdate")
    public Result<Map<String, String>> announcementsUpdate(@RequestBody Announcements announcements) {
        int num = announcementService.announcementsUpdate(announcements);
        Map<String, String> map = new HashMap<>();
        if (num > 0) {
            map.put("result", "true");
        } else {
            map.put("result", "false");
        }
        return Result.ok(map);
    }

    @GetMapping("/getUserAll")
    public Result<List<Users>> getUserAll(){
        return Result.ok(announcementService.getUserAll());
    }
}
